//
//  YTAPIClient.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/6.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTAPIClient.h"

@implementation YTAPIClient

+ (RACSignal *)requestWithURL:(NSString *)url
                    paramters:(NSDictionary *)dict {
    return [self requestWithURL:url paramters:dict isShowError:YES];
}

+ (RACSignal *)requestWithURL:(NSString *)url
                    paramters:(NSDictionary *)dict
                  isShowError:(BOOL)isShow {
    return [self requestWithURL:url paramters:dict isShowError:isShow isError:YES];
}

+ (RACSignal *)requestWithURL:(NSString *)url
                    paramters:(NSDictionary *)dict
                  isShowError:(BOOL)isShow
                      isError:(BOOL)isError {
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        return [[RACScheduler scheduler] schedule:^{
            @strongify(self);
            if (dict) {
                [self requestMethodWithURL:url
                                 paramters:dict
                               isShowError:isShow
                                   isError:isError
                                subscriber:subscriber];
            } else {
                [self requestMethodWithURL:url
                                 paramters:[NSDictionary dictionary]
                               isShowError:isShow
                                   isError:isError
                                subscriber:subscriber];
            }
        }];
    }];
}

#pragma mark - private

/**
 基础方法

 @param url 路由
 @param dict 参数
 @param isShow 是否显示error
 @param subscriber 订阅
 */
+ (void)requestMethodWithURL:(NSString *)url
                   paramters:(NSDictionary *)dict
                 isShowError:(BOOL)isShow
                     isError:(BOOL)isError
                  subscriber:(id<RACSubscriber>)subscriber {
    [[YTNetManager manager] postWithURL:url parameters:dict success:^(id response) {
        
        switch ([response[@"resultCode"] integerValue]) {
            case 1000: {
                [subscriber sendNext:response];
                [subscriber sendCompleted];
            }
                break;
            case 1011: {
                [[NSNotificationCenter defaultCenter] postNotificationName:YTUserNeedToLoginNotification object:nil];
            }
                break;
            default: {
                // 显示错误信息
                if (isShow) {
                    [self showTips:response[@"resultMessage"]];
                }
                // 这里的错误是内部错误,但是以error的形式发出去
                if (isError) {
                    [subscriber sendError:[NSError errorWithDomain:url
                                                              code:[response[@"resultCode"] integerValue]
                                                          userInfo:response]];
                } else {
                    /// 部分后台错误信息需要传递出去
                    [subscriber sendNext:response];
                    [subscriber sendCompleted];
                }
            }
                break;
        }
    } failure:^(NSError *error) {
        [subscriber sendError:error];
    }];
}

+ (void)showTips:(NSString *)tips {
    [UIAlertView bk_showAlertViewWithTitle:@"友情提示"
                                   message:tips
                         cancelButtonTitle:@"确认" otherButtonTitles:nil
                                   handler:^(UIAlertView *alertView, NSInteger buttonIndex) {}];
}

@end
